<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
include '../../connection/connection.php';
include "../../value/value_kr.php";
$monitor = $_REQUEST['monitor'];
$monitorname = $_REQUEST['monitorname'];
	
$selectDVR = mysql_query('select * from dvr', $connection);
if ( !isset($_REQUEST['page'] ))
	{
    	$page = 1 ;
	}else{
		$page = $_REQUEST['page'] ;
	}
	
	$list=$_SESSION['sort'];
	$str=$_GET['str'];
	$_SESSION['sortn']=$str;
	$flagDesc=$_GET['flagDesc'];
	
		$list["cf.id"]="DESC";
		$list["cf.name"]="DESC";
		$list["cf.host"]="DESC";
		$list["cf.pos_x"]="DESC";
		$list["cf.pos_y"]="DESC";
		$list["cf.go_mode"]="DESC";
		$list["cf.DVR"]="DESC";
		$list["cf.IP"];
		$list["cf.Port"]="DESC";
		$list["cf.UserID"]="DESC";
		$list["cf.PW"] = "DESC";
		$list["cf.live_time"] = "DESC";
		
		$str="cf.live_time";
		
		$_SESSION['sortn']=$str;
		$_SESSION['sort']=$list;
		
$sql = 'SELECT cf.id,cf.`name`,cf.`host`,cf.pos_x,cf.pos_y,cf.go_mode,cf.DVR,cf.IP,cf.`Port`,cf.UserID,cf.PW FROM `configcamera` cf ';
$selectConfcamera = mysql_query($sql.' ORDER BY '.$str.' '.$list[$str], $connection);
$_SESSION['sort']=$list;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=edge" />
<link href="../../css/style.css" rel="stylesheet" type="text/css" />
<link href="../../popup-window.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../js/jquery.js"></script>
<script type="text/javascript" src="../../popup-window.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	ScrollTable();
	
});
function MyResize(){
	ScrollTable();
}

function ScrollTable(){
	wid = $("table#tablebody2").parent().css('width');
	wid = wid.slice(0,-2)-17;
	$("table#tablebody2").css("width", wid);
	$("table#tablebodyheader").css("width", wid);
	$('table#tablebody2').parent().scrollTop(0);
	OnTRCSS();
}
function OnTRCSS(){
	$("table.tbchung tr:even").each(function(){
		$(this).css("background-color","#eff3fa");
	});
}
</script>
<script type="text/javascript">
$(function() {
	 var checkexistus = '<?=$_SESSION['iduser']?>';
	if(checkexistus==''){
		admin_logout();
		}
});

function admin_logout(){
	window.location="../../view_logout.php?changepage=admin";
}
function onclickSearch(){
	window.location = "../../search/searchcarnums.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>";
}
function onclickView(){
	window.location = "../../index.php?viewaction=none";
}

function loadAllCamera(){
	$.ajax({url:"loadAllCamera.php",success:function(result){
		document.getElementById('tableconfigcamera').innerHTML = result;
		ScrollTable();
		}
	});
}
function resetConfigCamera(){
	document.getElementById('txtcamera').value = "";
	document.getElementById('txtnamecamera').value = "";
	document.getElementById('txtserver').value = "";
	document.getElementById('txtposx').value = "";
	document.getElementById('txtposy').value = "";
	document.getElementById('txtIPconf').value = "";
	document.getElementById('txtPort').value = "";
	document.getElementById('txtUserID').value = "";
	document.getElementById('txtPW').value = "";

	document.getElementById('txtcamera').style.background = "#FFFFFF";
	document.getElementById('txtcamera').readOnly = false;

	document.getElementById('buttonadd').setAttribute("class","inbtn");
	document.getElementById('buttonadd').className = "inbtn";
	
	document.getElementById('buttonedit').setAttribute("class","inbtnDisable");
	document.getElementById('buttondelete').setAttribute("class","inbtnDisable");
	document.getElementById('buttonedit').className = "inbtnDisable";
	document.getElementById('buttondelete').className = "inbtnDisable";
	document.getElementById('buttonadd').disabled = false;
	document.getElementById('buttonedit').disabled = true;
	document.getElementById('buttondelete').disabled = true;
}
function onclickTableRow(els,idconfigcamera){
	$('#tablebody2 tr.select').removeClass('select');
	$(els).addClass('select');
	
	document.getElementById('idrowconfcamera').value = idconfigcamera;
	document.getElementById('idconfig_camera').value = idconfigcamera;
	$.ajax({url:"ajax-detail-content-camera.php?idconfigcamera="+idconfigcamera,success:function(result){
		document.getElementById('idtableconfigcamera').innerHTML = result;

		document.getElementById('buttonadd').setAttribute("class","inbtnDisable");
		document.getElementById('buttonadd').className = "inbtnDisable";
		document.getElementById('buttonedit').setAttribute("class","inbtn");
		document.getElementById('buttonedit').className = "inbtn";
		document.getElementById('buttondelete').setAttribute("class","inbtn");
		document.getElementById('buttondelete').className = "inbtn";

		document.getElementById('buttonadd').disabled = true;
		document.getElementById('buttonedit').disabled = false;
		document.getElementById('buttondelete').disabled = false;
		}
	});
}
function edit_configCamera(){ 
	var idconfig_camera = document.getElementById('idconfig_camera').value;
	var cameraname =  document.getElementById('txtnamecamera').value;
	var servername = document.getElementById('txtserver').value;
	var mode = document.getElementById('mode').value;
	var postx = document.getElementById('txtposx').value;
	var posty = document.getElementById('txtposy').value;
	var dvr = document.getElementById('txtDVR').value;
	var ipconf = document.getElementById('txtIPconf').value;
	var port =  document.getElementById('txtPort').value;
	var userid = document.getElementById('txtUserID').value;
	var pw = document.getElementById('txtPW').value;

	$.ajax({url:"edit-configCamera.php?idconfig_camera="+idconfig_camera+"&cameraname="+cameraname+"&servername="+servername+"&postx="+postx+"&posty="+posty+"&ipconf="+ipconf+"&port="+port+"&userid="+userid+"&pw="+pw+"&dvr="+dvr+"&mode="+mode, success:function(result){
		document.getElementById('tableconfigcamera').innerHTML = result;
		ScrollTable();
		alert("<?=$l_alerteditsuccessful?>");
		}
	});
} 

function delete_configCamera(){
	var idconfigcamera = document.getElementById('idconfig_camera').value;
	$.ajax({url:"delete-configCamera.php?idconfigcamera="+idconfigcamera,success:function(result){
		document.getElementById('tableconfigcamera').innerHTML = result;
		ScrollTable();
		alert("<?=$l_alertdeletesuccessful?>");
		resetConfigCamera();
		
		}
	});
}
function onclickAddCamera(){
	var idcamera = $('#txtcamera').val();
	var name =  $('#txtnamecamera').val();
	var host =  $('#txtserver').val();
	var postx = $('#txtposx').val();
	var posty = $('#txtposy').val();
	var dvr = $('#txtDVR').val();
	var ip = $('#txtIPconf').val();
	var port = $('#txtPort').val();
	var mode = $('#mode').val();
	var userid = $('#txtUserID').val();
	var pw = $('#txtPW').val();

	if(idcamera.length==0){
		alert('camera id is not null');
		return false;
	}else if(name.length==0){
		alert('name is not null');
		return false;
	}else if(host.length==0){
		alert('host is not null');
		return false;
	}

	$.ajax({url:"validate_configcamera.php?idcamera="+idcamera,success: function(result){
		//alert(result);
		document.getElementById('dvidcamerahidden').innerHTML = result;
		var results = document.getElementById('idcamerahidden').value;
		if(results=='no'){
	   		alert('camera id already existed');
   		}else{
   	   		$.ajax({url:"insert_configcamera.php",data:{idcamera:idcamera,name:name,host:host,postx:postx,posty:posty,dvr:dvr,ip:ip,port:port,mode:mode,userid:userid,pw:pw},success:function(resultadd){
	   	   		document.getElementById('tableconfigcamera').innerHTML = resultadd;
				alert('<?=$l_alertinsertsuccessful?>');
				ScrollTable();
				resetConfigCamera();
   	   	   		}
	   	   		});
   		}
   		
		}
	});
}
// sort
function sortAndPagingConfigCamera(string,flagDesc){
	$.ajax({url:"sortAndPagingConfigCamera.php?string="+string+"&flagDesc="+flagDesc,success:function(result){
		var expr = new RegExp('>[ \t\r\n\v\f]*<', 'g');
		var response_html_fixed = result.replace(expr, '><');
		document.getElementById('tableconfigcamera').innerHTML = response_html_fixed;
		ScrollTable();
		}
	});
}

</script>
<?php include 'show_function_popup_carcrime.php';?>
</head>
<body onresize="MyResize();" onload="load_popup_carCrime();">
<p id="maylya" ><input type="hidden"  id="mayly" name="mayly" />
 </p>
 <div id="dvidcamerahidden" style="display: none;"><input type="hidden" id="idcamerahidden"></div>
   <div id="menu" style="text-align: center;">
    	<ul class="amenu">
        	<li><a onclick="onclickSearch();"><input class="csssearch" type="button"/></a></li>
            <li style="background:#1e7bbd"><a href=""><input class="cssadminchoose" type="button"/></a></li>
            <li style="width: 215px;"><a onclick="onclickView();"><input class="cssview" type="button"/></a>
             	<select class="seltopadmin" disabled="disabled">
            		<option><?php echo $monitorname;?></option>
            	</select>
            </li>
            <li><a onclick="admin_logout()" style="cursor:pointer" ><input class="csslogout" type="button"/></a></li>
        </ul>
         <div class="clear"></div>
    </div>
     <div id="submenu">
    	<ul class="asubmenu">
        	<li><a href="../content-monitor-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_monitor?></a></li>
            <li><a href="#" style="background:#034776; border-radius:6px"><?php echo $l_camera?></a></li>
           <li><a href="../car-crime-input-by-user/car-crime-input-by-user-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_carcrimeuser?></a></li>
            <li><a href="../car-crime/content-car-crime-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>"><?php echo $l_carcrime?></a></li>
            <li><a href="../PC/content-PC-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>">PC</a></li>
            <li><a href="../user/content-user-admin.php?monitor=<?=$monitor?>&monitorname=<?=$monitorname?>" ><?php echo $l_user?></a></li>
        </ul>
        <div class="clear"></div>
    </div>
    <div class="main" >
    	<div id="idtableconfigcamera" style="width: 100%;min-width:624px;">
		    <table  cellpadding="0" cellspacing="0" border="0" width="100%" class="tbm1">
		    	<tr>
		    		<td>ID :</td>
		    		<td> <input type="text" id="txtcamera" name="txtcamera" style="width: 95px;padding-left: 5px;line-height: 26px;"></td>
		    		<td><?php echo $l_name?> :</td> 
		    		<td><input type="text" id="txtnamecamera" name="txtnamecamera" style="width: 200px;padding-left: 5px;line-height: 26px;"></td>
		    		<td><?php echo $l_server?>: </td> 
		    		<td><input type="text" id="txtserver" name="txtserver" style="width: 300px;padding-left: 5px;line-height: 26px;"> </td>
		    		<td>pos_x: </td> 
		    		<td><input type="text" id="txtposx" name="txtposx"  style="width: 150px;padding-left: 5px;line-height: 26px;"></td>
		    		<td>pos_y: </td> 
		    		<td><input type="text" id="txtposy" name="txtposy"  style="width: 150px;padding-left: 5px;line-height: 26px;"></td>
		    	</tr>
		    	
		    	<tr>
		    		<td>DVR :</td>
		    		<td> <select size="1" name="txtDVR" id="txtDVR" style="width: 95px;height: 26px;padding-top:3px;padding-left: 5px;">
		    			<?php while ($rowdvr = mysql_fetch_array($selectDVR)){?><option style="padding: 5px;" value="<?=$rowdvr[id]?>" id="<?=$rowdvr[id]?>"><?=$rowdvr[name]?></option>
		    			<?php }?></select></td>
		    		<td>IP :</td>  
		    		<td><input type="text" id="txtIPconf" name="txtIPconf" style="width: 200px;padding-left: 5px;line-height: 26px;"></td>
		    		<td>Port: </td> 
		    		<td><input type="text" id="txtPort" name="txtPort" style="padding-left: 5px;line-height: 26px;">&nbsp;&nbsp;&nbsp;&nbsp;
		    			<?php echo $l_mode?>:&nbsp;&nbsp;&nbsp;<select size="1" name="mode" id="mode" style="width: 85px;height: 26px;padding-top:3px;">
		    				<option style="padding: 5px;"><?php echo $l_go?></option>
		    				<option style="padding: 5px;"><?php echo $l_come?></option> </select> 
		    		</td>
		    		<td>UserID: </td> 
		    		<td><input type="text" id="txtUserID" name="txtUserID" style="width: 150px;padding-left: 5px;line-height: 26px;"></td>
		    		<td>PW: </td> 
		    		<td><input type="password" id="txtPW" name="txtPW" style="width: 150px;padding-left: 5px;line-height: 26px;"></td>
		    	</tr>
		    	<tr>
		    		<td colspan="12" style="right: 50px">
			    		 <div style="height: 10px;clear: both;"></div>
					     <div style="right: 50px;display: inline-block;top: 10px;float: right;">
					    	<input type="button" class="inbtn" style="width: 65px;" value="<?=$l_new?>" id="buttonadd" name="buttonadd" onclick="onclickAddCamera();"> 
					    	<input type="button"  class="inbtnDisable" style="width: 65px;"  value="<?php echo $l_edit?>"  id="buttonedit" name="buttonedit"  disabled="disabled" onclick="edit_configCamera();">
					    	<input type="button" class="inbtnDisable" style="width: 65px;"  value="<?php echo $l_delete?>" id="buttondelete" name="buttondelete" disabled="disabled"
					    	 onclick="if(confirm('<?=$l_areyousure?>')){ delete_configCamera();return true;}else{return false;}">
					    	<input type="button" class="inbtn" style="width: 65px;"  value="<?php echo $l_reset?>" onclick="resetConfigCamera();">
				  		  </div>
		    		</td>
		    	</tr>
		    </table>
	   </div>
	    
	    <div style="height: 10px;clear: both;"></div>
	     <div class="divtheader">
            	<table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung" id="tablebodyheader">
	            	<thead>
				     	<tr>
				     		<th style="width: 4%">No</th>
				     		<th style="width: 5%"><a onclick="sortAndPagingConfigCamera('cf.id',1)" style="cursor: pointer;color: #027acc">ID</a></th>
				     		<th style="width: 18%"><a onclick="sortAndPagingConfigCamera('cf.name',1)" style="cursor: pointer;color: #027acc"><?php echo $l_name?></a></th>
				     		<th style="width: 6%"><a onclick="sortAndPagingConfigCamera('cf.pos_x',1)" style="cursor: pointer;color: #027acc">pos_x</a></th>
				     		<th style="width: 6%"><a onclick="sortAndPagingConfigCamera('cf.pos_y',1)" style="cursor: pointer;color: #027acc">pos_y</a></th>
				     		<th style="width: 6%"><a onclick="sortAndPagingConfigCamera('cf.go_mode',1)" style="cursor: pointer;color: #027acc"><?php echo $l_mode?></a></th>
				     		<th style="width: 20%"><a onclick="sortAndPagingConfigCamera('cf.host',1)" style="cursor: pointer;color: #027acc"><?php echo $l_server?></a></th>
				     		<th style="width: 4%"><a onclick="sortAndPagingConfigCamera('cf.DVR',1)" style="cursor: pointer;color: #027acc">DVR</a></th>
				     		<th style="width: 10%"><a onclick="sortAndPagingConfigCamera('cf.IP',1)" style="cursor: pointer;color: #027acc">IP</a></th>
				     		<th style="width: 6%"><a onclick="sortAndPagingConfigCamera('cf.Port',1)" style="cursor: pointer;color: #027acc">Port</a></th>
				     		<th style="width: 9%"><a onclick="sortAndPagingConfigCamera('cf.UserID',1)" style="cursor: pointer;color: #027acc">UserID</a></th>
				     		<th style="width: 6%" class="last"><a onclick="sortAndPagingConfigCamera('cf.PW',1)" style="cursor: pointer;color: #027acc">PW</a></th>
			     		</tr>
		     		</thead>
            	</table>
	    </div>
	    <div  id="tableconfigcamera" style="width: 100%;">
	    <div style="overflow:scroll;overflow-x: hidden; width:100%; height:480px; background-color: #ffffff">
		     <table cellpadding="0" cellspacing="0" border="0" width="100%" class="tbchung"  id="tablebody2">
	     		<tbody>
					<?php 
                			$i = 0; 
			     		while ($row = mysql_fetch_array($selectConfcamera)){ 
			     			$i ++;?>
				     	<tr id="idrowconfcamera" onclick="onclickTableRow(this,<?=$row['id']?>);">
				     		<td style="text-align: center;width: 4%"><?=$i;?></td>
				     		<td style="text-align: center;width: 5%"><?=$row['id']?></td>
				     		<td style="width: 18%"><?=$row['name']."&nbsp;"?></td>
				     		<td style="width: 6%"><?=$row['pos_x']."&nbsp;"?></td>
				     		<td style="width: 6%"><?=$row['pos_y']."&nbsp;"?></td>
				     		<td style="text-align: center;width: 6%"><?if ($row['go_mode']==1){echo $l_come."&nbsp;";}else{echo $l_go."&nbsp;";} ?></td>
				     		<td style="width: 20%"><?=$row['host']."&nbsp;"?></td>
				     		<td style="text-align: center;width: 4%"><?=$row['DVR']."&nbsp;"?></td>
				     		<td style="width: 10%"><?=$row['IP']."&nbsp;"?></td>
				     		<td style="text-align: center;width: 6%"><?=$row['Port']."&nbsp;"?></td>
				     		<td style="width: 9%"><?=$row['UserID']."&nbsp;"?></td>
				     		<td style="text-align: center;width: 6%" class="last">
				     		<?php if (strlen($row['PW'])==0){echo "&nbsp;";}else{$vals="*"; for ($m=0;$m <strlen($row['PW']);$m++){?><?=$vals++;?><?php }}?></td>
				     	</tr>
			     	<?php 
			     			}
			     			if ($i>0 and $i<17) {
							 	foreach(range(0,17-$i) as $real){
							 		?>
							 		<tr>
								 		<td style="width:4%">&nbsp;</td>
								 		<td style="width:5%">&nbsp;</td>
								 		<td style="width:18%">&nbsp;</td>
									 	<td style="width:6%">&nbsp;</td>
									 	<td style="width:6%">&nbsp;</td>
									 	<td style="width: 6%;">&nbsp;</td>
									 	<td style="width:20%">&nbsp;</td>
								 		<td style="width:54%">&nbsp;</td>
								 		<td style="width:10%">&nbsp;</td>
									 	<td style="width:6%">&nbsp;</td>
									 	<td style="width:9%">&nbsp;</td>
									 	<td class="last" style="width: 6%;border-right: none !important;">&nbsp;</td>
								 	</tr>
							 	<?php }
							}?>
		     	</tbody>
		     </table>
		     </div>
		     </div>
    </div>
     <input type="hidden" name="idconfig_camera" id="idconfig_camera" value="">
     <?php include 'show_content_popup_carcrime.php';?>
</body>
 <?php mysql_close($connection);?>